package com.cloudcc.mobile.view.main.fragment;

import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.cloudcc.cloudframe.CApplication;
import com.cloudcc.cloudframe.bus.EventEngine;
import com.cloudcc.cloudframe.bus.MenuToggleEvent;
import com.cloudcc.cloudframe.model.MenuModel;
import com.cloudcc.cloudframe.model.MenuTabModel;
import com.cloudcc.cloudframe.util.GsonUtil;
import com.cloudcc.cloudframe.util.ListUtils;
import com.cloudcc.cloudframe.util.StringUtils;
import com.cloudcc.mobile.AppContext;
import com.cloudcc.mobile.R;
import com.cloudcc.mobile.adapter.RecyGridAdapter;
import com.cloudcc.mobile.event.refresh.RefreshMainMenuEvent;
import com.cloudcc.mobile.manager.CacheManager;
import com.cloudcc.mobile.manager.MenuManager;
import com.cloudcc.mobile.manager.RunTimeManager;
import com.cloudcc.mobile.manager.UrlManager;
import com.cloudcc.mobile.manager.UserManager;
import com.cloudcc.mobile.search.SearchActivity;
import com.cloudcc.mobile.util.UrlTools;
import com.cloudcc.mobile.view.activity.BusinesscardScanningActivity;
import com.cloudcc.mobile.view.activity.NewMenuFilterActivity;
import com.cloudcc.mobile.view.activity.TaskManagementActivty;
import com.cloudcc.mobile.view.base.BaseFragment;
import com.cloudcc.mobile.view.main.MainCustomActivity;
import com.cloudcc.mobile.view.main.MainUIActivity;
import com.cloudcc.mobile.view.mymodel.mydaishenpi;
import com.cloudcc.mobile.view.mymodel.shouyerefrence;
import com.cloudcc.mobile.view.schedule.ScheduleMainActivity;
import com.cloudcc.mobile.view.web.NewWebView;
import com.cloudcc.mobile.view.web.WebViewActivtiy;
import com.google.gson.Gson;
import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonParser;
import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.RequestParams;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest.HttpMethod;
import com.litesuits.android.log.Log;
import com.mypage.model.MainApproNum;
import com.mypage.utils.ListDataSave;
import com.mypage.utils.NetStateUtils;
import com.mypage.utils.SaveTemporaryData;
import com.cloudcc.mobile.bull.GongniuCheckinActivity;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import java.util.ListIterator;
import java.util.Map;
import java.util.TreeMap;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * @author wangzhiqiang
 * @Description: 主页九宫格样式 的fragment
 * @date 2015-10-26 上午11:10:02
 */
public class MainUIFragment extends BaseFragment implements Serializable {
    public int mF;//快速体验传值
    private static final MenuTabModel MenuTabModel = null;
    private String mEns = RunTimeManager.getInstance().getlanguage();
    private String language;//保存当前本地语言是中文还是英文
    private RecyclerView mGridView;

    ListDataSave datasave;

    @Override
    public LayoutInflater getLayoutInflater(Bundle savedInstanceState) {
        return super.getLayoutInflater(savedInstanceState);
    }

    @Bind(R.id.menu_filter)
    ImageView menu_filter;
    @Bind(R.id.backCustomKanBan)
    TextView backCustomKanBan;
    @Bind(R.id.message_num_tz)
    TextView message_num_tz;
    @Bind(R.id.message_num_99)
    TextView message_num_99;
    //    private FunGridAdapter mAdapter;
    private RecyGridAdapter mAdapter;
    private List<MenuTabModel> menuTabsAll;// 所有该显示的

    private List<MenuModel> menusend;// 所有的
    private List<MenuTabModel> currentShowList;// 当前九宫格显示的

    private int mCount;// 记录默人加载的数据
    // protected CustomProgressDialog mProgress;
    // private boolean _isVisible = false;// 控制bindtoken影响的问题
    Boolean isFirstIn = true;
    //判断是否缓存选项卡
    Map<String, Boolean> map = new TreeMap<String, Boolean>();
    private FrameLayout frameLayout;
    private MenuTabModel addmore;


    @Override
    public void init() {
        SaveTemporaryData.isEventAndTask = false;
        // mProgress = CustomProgressDialog.createDialog(mContext);
        SaveTemporaryData.detailDyamic = "";
        showWaintingno();
        isLogin();
        isfirst();
        super.init();
        EventEngine.register(this);
        initView();
        initData();
        initCustom();

    }

    public void isfirst() {
        mF = ((MainUIActivity) getActivity()).setInt();

        SharedPreferences pref = mContext.getSharedPreferences(RunTimeManager.getInstance().getUserId(), 0);
        //取得相应的值，如果没有该值，说明还未写入，用true作为默认值
        isFirstIn = pref.getBoolean("isFirstIn", true);
        language = pref.getString("language", "zh");
        frameLayout = (FrameLayout) findViewById(R.id.tishi);
//		map.put("isFirstIn", isFirstIn);
    }

    private void initCustom() {
        ((ViewGroup) findViewById(R.id.backCustom).getParent())
                .setVisibility(TextUtils.isEmpty(UserManager.getManager()
                        .getUser().appMainPage) ? View.GONE : View.VISIBLE);
    }

    private RelativeLayout search_kuang;

    private void initView() {
        mGridView = (RecyclerView) findViewById(R.id.gridview);
        search_kuang = (RelativeLayout) findViewById(R.id.sousuo_kuang);
        datasave = new ListDataSave(mContext, "baiyu");
        search_kuang.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(mContext, SearchActivity.class);
                Bundle bundle = new Bundle();
                bundle.putSerializable("sort.filter", (Serializable) menusend);
                intent.putExtras(bundle);
                startActivity(intent);
            }
        });

    }

    public void initData() {
        menuTabsAll = new ArrayList<MenuTabModel>();
        menuTabsAll.clear();
        menusend = new ArrayList<MenuModel>();
        menusend = ((MainUIActivity) getActivity()).getSendmenus();
        currentShowList = new ArrayList<MenuTabModel>();
        currentShowList.clear();

        MenuTabModel tabSchedule = new MenuTabModel();
        tabSchedule.id = "task1201";
        tabSchedule.url = "url:task";
        tabSchedule.tab_style = "style:task";
        tabSchedule.tabStyle = "style:task";
        if ("en".equals(mEns)) {
            tabSchedule.tabName = "Schedules";
            tabSchedule.tab_name = "Schedules";
        } else {
            tabSchedule.tabName = "日程事件";
            tabSchedule.tab_name = "日程事件";
        }

        tabSchedule.imageId = "cloudtabtask";
        tabSchedule.objId = "android:task";

        MenuTabModel tabApproval = new MenuTabModel();
        tabApproval.id = "approval1201";
        tabApproval.url = "url:approval";
        tabApproval.tab_style = "style:approval";
        tabApproval.tabStyle = "style:approval";
        if ("en".equals(mEns)) {
            tabApproval.tabName = "Approvals";
            tabApproval.tab_name = "Approvals";
        } else {
            tabApproval.tabName = "待审批";
            tabApproval.tab_name = "待审批";
        }

        tabApproval.imageId = "cloudtab_approval";
        tabApproval.objId = "android:approval";

        MenuTabModel tabScanCard = new MenuTabModel();
        tabScanCard.id = "card1201";
        tabScanCard.url = "url:card";
        tabScanCard.tab_style = "style:card";
        tabScanCard.tabStyle = "style:card";
        tabScanCard.tabName = getString(R.string.home_saoyisao);
        tabScanCard.tab_name = getString(R.string.home_saoyisao);
        tabScanCard.imageId = "cloudtabscancard";
        tabScanCard.objId = "android:card";


        MenuTabModel tabCeshi4 = new MenuTabModel();
        tabCeshi4.id = "weixsld1201";
        tabCeshi4.url = "url:weixsld";
        tabCeshi4.tab_style = "style:weixsld";
        tabCeshi4.tabStyle = "style:weixsld";
        tabCeshi4.tabName = getString(R.string.home_xiaoshou);
        tabCeshi4.tab_name = getString(R.string.home_xiaoshou);
        tabCeshi4.imageId = "cloudtab_weixsld";
        tabCeshi4.objId = "android:weixsld";

        addmore = new MenuTabModel();
        addmore.objId = "android:addmore";
        addmore.tabName = getString(R.string.home_add);
        addmore.tab_name = getString(R.string.home_add);


        if (22 == mF) {
            backCustomKanBan.setVisibility(View.GONE);
            menuTabsAll.add(tabSchedule);
            menuTabsAll.add(tabScanCard);
            menuTabsAll.add(tabCeshi4);
//ceo
        } else if (11 == mF) {
            backCustomKanBan.setVisibility(View.VISIBLE);
            menuTabsAll.add(tabSchedule);
            menuTabsAll.add(tabCeshi4);
        } else {
            backCustomKanBan.setVisibility(View.GONE);
            menuTabsAll.add(tabSchedule);
            menuTabsAll.add(tabApproval);
        }
        // 获得ccmenu
        // mGridView.setClickable(false);
        MenuModel menuModel = getMainMenu();
        if (menuModel != null && !ListUtils.isEmpty(menuModel.tab)) {
            menuTabsAll.addAll(menuModel.tab);

        }
        List<MenuTabModel> ingoreMenus = CacheManager.getInstance()
                .getIgnoreMenu();
        if (isFirstIn) {
            ListIterator<MenuTabModel> iterator = menuTabsAll.listIterator();

            while (iterator.hasNext()) {
                MenuTabModel model = iterator.next();
                if ((!ListUtils.isEmpty(ingoreMenus))
                        && ingoreMenus.contains(model)) {
                    continue;
                }
                currentShowList.add(model);
                mCount = currentShowList.size();
            }
        } else {
            currentShowList = (datasave.getDataList(RunTimeManager.getInstance().getUserId()));
            if (!language.equals(mEns)) {//语言切换（中英文）
                SharedPreferences pref = mContext.getSharedPreferences(RunTimeManager.getInstance().getUserId(), 0);
                Editor editor = pref.edit();
                editor.putString("language", mEns);
                editor.commit();

                for (MenuTabModel menuTabModel : currentShowList) {
                    if (menusend != null && menusend.size() != 0) {
                        for (MenuModel menuModel1 : menusend) {
                            if (menuModel1.tab != null && menuModel1.tab.size() != 0) {
                                for (MenuTabModel menuTabModel12 : menuModel1.tab) {
                                    if (menuTabModel.id.equals(menuTabModel12.id)) {
                                        menuTabModel.tab_name = menuTabModel12.tab_name;
                                    }
                                    if (menuTabModel.id.equals("task1201")) {
                                        menuTabModel.tab_name = tabSchedule.tab_name;
                                    }
                                    if (menuTabModel.id.equals("approval1201")) {
                                        menuTabModel.tab_name = tabApproval.tab_name;
                                    }
                                    if (menuTabModel.id.equals("card1201")) {
                                        menuTabModel.tab_name = tabScanCard.tab_name;
                                    }
                                    if (menuTabModel.id.equals("weixsld1201")) {
                                        menuTabModel.tab_name = tabCeshi4.tab_name;
                                    }
                                }
                            }
                        }
                    }

                }
            }
            if (currentShowList.size() <= 0) {
                frameLayout.setVisibility(View.VISIBLE);
            } else {
                frameLayout.setVisibility(View.GONE);
            }
        }
        if (!addmore.objId.equals(currentShowList.get(currentShowList.size() - 1).objId)) {//显示的list集合里面不包含addmore
//        if (!currentShowList.get(currentShowList.size() - 1).objId.equals(addmore.objId)) {//显示的list集合里面不包含addmore
            currentShowList.add(addmore);
        }
        GsonUtil.Object2Json(currentShowList);
        mAdapter = new RecyGridAdapter(mContext, currentShowList);
//        mAdapter.changeData(currentShowList);
        mAdapter.setOnItemClickLitener(new RecyGridAdapter.OnItemClickLitener() {
            @Override
            public void onItemClick(View view, int position) {
                AppContext.iscomeMenu = true;
                try {
                    if (ListUtils.isEmpty(currentShowList)) {
                        return;
                    }

                    MenuTabModel menuTabModel = currentShowList.get(position);
                    MenuModel mainMenu = getMainMenu();
                    Log.d("kaoqinceshi", "id" + " " + mainMenu.id + "label" + "  "
                            + mainMenu.label + menuTabModel.objId);
                    if (StringUtils.isNotBlank(menuTabModel.objId)
                            && menuTabModel.objId.startsWith("android:")) {
                        if (menuTabModel.objId.startsWith("android:addmore")) {//控制选项卡界面
                            onClickMenuFilter();

                        } else if (menuTabModel.objId.startsWith("android:task")) {// 任务
                            Intent intent = new Intent(getActivity(),
                                    ScheduleMainActivity.class);
                            startActivity(intent);
                        } else if (menuTabModel.objId.startsWith("android:approval")) {
                            // Intent intent2 = new Intent(getActivity(),
                            // TaskManagementActivty.class);
                            AppContext.indexUrl = UrlManager.getRootUrl()
                                    + UrlTools
                                    .getModuleUrl(mainMenu.id, mainMenu.label);
                            AppContext.urlString = UrlManager.getRootUrl()
                                    + "/WeiXin_getmangeAllAppRequest.action";
//                    Intent intent2 = new Intent(getActivity(), NewWebView.class);
                            Intent intent2 = new Intent(getActivity(), TaskManagementActivty.class);
                            startActivity(intent2);
                        } else if (menuTabModel.objId.startsWith("android:card")) {
//                    Intent intent = new Intent(getActivity(),
//                            Myinformation.class);
                            Intent intent = new Intent(getActivity(),
                                    BusinesscardScanningActivity.class);
                            startActivity(intent);
                        } else if (menuTabModel.objId.startsWith("android:weixsld")) {
                            AppContext.indexUrl = UrlManager.getRootUrl()
                                    + UrlTools
                                    .getModuleUrl(mainMenu.id, mainMenu.label);
                            AppContext.urlString = UrlManager.getRootUrl()
                                    + "/weistdashbord.action?m=weixsld";
                            Intent intent = new Intent(getActivity(),
                                    WebViewActivtiy.class);
                            startActivity(intent);
                        } else if (menuTabModel.objId.startsWith("android:mywork")) {
                            AppContext.indexUrl = UrlManager.getRootUrl()
                                    + UrlTools
                                    .getModuleUrl(mainMenu.id, mainMenu.label);
                            AppContext.urlString = UrlManager.getRootUrl()
                                    + "/wx_taskquery.action?m=mywork";
                            Intent intent = new Intent(getActivity(), NewWebView.class);
                            // Intent intent = new Intent(getActivity(),
                            // WebViewActivtiy.class);

                            startActivity(intent);
                        } else if (menuTabModel.objId.startsWith("android:newOpp")) {
                            AppContext.indexUrl = UrlManager.getRootUrl()
                                    + UrlTools
                                    .getModuleUrl(mainMenu.id, mainMenu.label);
                            AppContext.urlString = UrlManager.getRootUrl()
                                    + "/newlead.action?m=newOpp";
                            Intent intent = new Intent(getActivity(),
                                    WebViewActivtiy.class);
                            startActivity(intent);
                        } else if (menuTabModel.objId.startsWith("android:keyProjects")) {


                            AppContext.indexUrl = UrlManager.getRootUrl()
                                    + UrlTools
                                    .getModuleUrl(mainMenu.id, mainMenu.label);
                            AppContext.urlString = UrlManager.getRootUrl()
                                    + "/opportunity.action?m=keyProjects";
                            Intent intent = new Intent(getActivity(),
                                    WebViewActivtiy.class);
                            startActivity(intent);
                        } else if (menuTabModel.objId.startsWith("android:myyj")) {
                            AppContext.indexUrl = UrlManager.getRootUrl()
                                    + UrlTools
                                    .getModuleUrl(mainMenu.id, mainMenu.label);
                            AppContext.urlString = UrlManager.getRootUrl()
                                    + "/salesCloudDashboard.action?m=myyj";
                            Intent intent = new Intent(getActivity(),
                                    WebViewActivtiy.class);
                            startActivity(intent);
                        }
                    } else if("20172C76372FCA6oKQBM".equals(menuTabModel.objId)){
                        Intent intent = new Intent(getActivity(),
                                GongniuCheckinActivity.class);
                        startActivity(intent);
                    }else {
                        Log.d("clickshanping", "闪屏测试:点击跳转第一步");
                        AppContext.indexUrl = UrlManager.getRootUrl()
                                + UrlTools.getModuleUrl(mainMenu.id, mainMenu.label);
                        AppContext.urlString = UrlManager.getRootUrl()
                                + UrlTools.getTabsUrl(menuTabModel, mainMenu);
                        MenuManager.getInstance().enterMenuDetailOrList(menuTabModel,
                                getActivity());
                    }
                } catch (Exception e) {
                    // TODO: handle exception
                }
            }

            @Override
            public void onItemLongClick(View view, int position) {

            }
        });
        mGridView.setAdapter(mAdapter);
//        mGridView.setOnItemClickListener(this);

        mGridView.setLayoutManager(new GridLayoutManager(mContext, 3));
        dismissDialogTy();
        try {
            if (menuModel.isshuaxin) {
                android.util.Log.d("ondissmiss", "传过来了2");
                menuModel.isshuaxin = false;
            } else if (!menuModel.isshuaxin) {
            }
        } catch (Exception e) {
            android.util.Log.d("ondissmiss", "传过来了3");

//			dismissWainting();
        }
        if (11 == mF) {
            if (!MenuManager.getInstance().isEntityMenuTab("/salesCloudDashboard.action?m=query")) {
                AppContext.indexUrl = UrlManager.getRootUrl() + "/WeiXin_getUserTabList.action?appId=ace000001";
                AppContext.urlString = UrlManager.getRootUrl() + "/weisalesCloudDashboard.action?m=query&rtnURL=WeiXinLeshi_index.action&pageType=boss";
                Intent intent = new Intent(getActivity(), WebViewActivtiy.class);
                getActivity().startActivity(intent);
            }
        }
    }

    public static <T> List<T> getObjectList(String jsonString, Class<T> cls) {
        List<T> list = new ArrayList<T>();
        try {
            Gson gson = new Gson();
            JsonArray arry = new JsonParser().parse(jsonString).getAsJsonArray();
            for (JsonElement jsonElement : arry) {
                list.add(gson.fromJson(jsonElement, cls));
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return list;
    }

    @Override
    public int getLayoutId() {
        return R.layout.main_main;
    }

    @OnClick(R.id.backCustomKanBan)
    public void backCustomKanBan() {
        if (!MenuManager.getInstance().isEntityMenuTab("/salesCloudDashboard.action?m=query")) {
            AppContext.indexUrl = UrlManager.getRootUrl() + "/WeiXin_getUserTabList.action?appId=ace000001";
            AppContext.urlString = UrlManager.getRootUrl() + "/weisalesCloudDashboard.action?m=query&rtnURL=WeiXinLeshi_index.action&pageType=boss";
            Intent intent = new Intent(getActivity(), WebViewActivtiy.class);
            getActivity().startActivity(intent);
        }
    }

    @OnClick(R.id.backCustom)
    public void clickEnterCustom() {
        Intent intent = new Intent(mContext, MainCustomActivity.class);
        intent.putExtra("from.type", 1);
        startActivity(intent);
    }

    public void onClickMenuFilter() {
        Log.d("changdushi", "长度+" + menuTabsAll.size() + ":::" + currentShowList.size());
        menusend = ((MainUIActivity) getActivity()).getSendmenus();
//		Log.d("slidingceshi", "数据chuandi"+menusend.size());
//		 Intent intent = new Intent(mContext, SearchActivity.class);
        Intent intent = new Intent(mContext, NewMenuFilterActivity.class);
        Bundle bundle = new Bundle();
        bundle.putSerializable("sort.filter", (Serializable) menusend);
        bundle.putSerializable("alsotrue", (Serializable) currentShowList);
        intent.putExtras(bundle);
        startActivity(intent);
    }

    @OnClick(R.id.chatmes_menubtn)
    public void onClickSlide() {
        EventEngine.post(new MenuToggleEvent(false, false));
    }

    @OnClick(R.id.menu_filter)
    public void onClickRight() {
        EventEngine.post(new MenuToggleEvent(false, true));
    }
//    @Override
//    public void onItemClick(AdapterView<?> parent, View view, int position,
//                            long id) {
//        AppContext.iscomeMenu = true;
//        try {
//            if (ListUtils.isEmpty(currentShowList)) {
//                return;
//            }
//
//            MenuTabModel menuTabModel = currentShowList.get(position);
//            MenuModel mainMenu = getMainMenu();
//            Log.d("kaoqinceshi", "id" + " " + mainMenu.id + "label" + "  "
//                    + mainMenu.label + menuTabModel.objId);
//            if (StringUtils.isNotBlank(menuTabModel.objId)
//                    && menuTabModel.objId.startsWith("android:")) {
//                if (menuTabModel.objId.startsWith("android:addmore")) {//控制选项卡界面
//                    onClickMenuFilter();
//
//                } else if (menuTabModel.objId.startsWith("android:task")) {// 任务
//                    Intent intent = new Intent(getActivity(),
//                            ScheduleMainActivity.class);
//                    startActivity(intent);
//                } else if (menuTabModel.objId.startsWith("android:approval")) {
//                    // Intent intent2 = new Intent(getActivity(),
//                    // TaskManagementActivty.class);
//                    AppContext.indexUrl = UrlManager.getRootUrl()
//                            + UrlTools
//                            .getModuleUrl(mainMenu.id, mainMenu.label);
//                    AppContext.urlString = UrlManager.getRootUrl()
//                            + "/WeiXin_getmangeAllAppRequest.action";
////                    Intent intent2 = new Intent(getActivity(), NewWebView.class);
//                    Intent intent2 = new Intent(getActivity(), TaskManagementActivty.class);
//                    startActivity(intent2);
//                } else if (menuTabModel.objId.startsWith("android:card")) {
////                    Intent intent = new Intent(getActivity(),
////                            Myinformation.class);
//                    Intent intent = new Intent(getActivity(),
//                            BusinesscardScanningActivity.class);
//                    startActivity(intent);
//                } else if (menuTabModel.objId.startsWith("android:weixsld")) {
//                    AppContext.indexUrl = UrlManager.getRootUrl()
//                            + UrlTools
//                            .getModuleUrl(mainMenu.id, mainMenu.label);
//                    AppContext.urlString = UrlManager.getRootUrl()
//                            + "/weistdashbord.action?m=weixsld";
//                    Intent intent = new Intent(getActivity(),
//                            WebViewActivtiy.class);
//                    startActivity(intent);
//                } else if (menuTabModel.objId.startsWith("android:mywork")) {
//                    AppContext.indexUrl = UrlManager.getRootUrl()
//                            + UrlTools
//                            .getModuleUrl(mainMenu.id, mainMenu.label);
//                    AppContext.urlString = UrlManager.getRootUrl()
//                            + "/wx_taskquery.action?m=mywork";
//                    Intent intent = new Intent(getActivity(), NewWebView.class);
//                    // Intent intent = new Intent(getActivity(),
//                    // WebViewActivtiy.class);
//
//                    startActivity(intent);
//                } else if (menuTabModel.objId.startsWith("android:newOpp")) {
//                    AppContext.indexUrl = UrlManager.getRootUrl()
//                            + UrlTools
//                            .getModuleUrl(mainMenu.id, mainMenu.label);
//                    AppContext.urlString = UrlManager.getRootUrl()
//                            + "/newlead.action?m=newOpp";
//                    Intent intent = new Intent(getActivity(),
//                            WebViewActivtiy.class);
//                    startActivity(intent);
//                } else if (menuTabModel.objId.startsWith("android:keyProjects")) {
//                    AppContext.indexUrl = UrlManager.getRootUrl()
//                            + UrlTools
//                            .getModuleUrl(mainMenu.id, mainMenu.label);
//                    AppContext.urlString = UrlManager.getRootUrl()
//                            + "/opportunity.action?m=keyProjects";
//                    Intent intent = new Intent(getActivity(),
//                            WebViewActivtiy.class);
//                    startActivity(intent);
//                } else if (menuTabModel.objId.startsWith("android:myyj")) {
//                    AppContext.indexUrl = UrlManager.getRootUrl()
//                            + UrlTools
//                            .getModuleUrl(mainMenu.id, mainMenu.label);
//                    AppContext.urlString = UrlManager.getRootUrl()
//                            + "/salesCloudDashboard.action?m=myyj";
//                    Intent intent = new Intent(getActivity(),
//                            WebViewActivtiy.class);
//                    startActivity(intent);
//                }
//            } else {
//                Log.d("clickshanping", "闪屏测试:点击跳转第一步");
//                AppContext.indexUrl = UrlManager.getRootUrl()
//                        + UrlTools.getModuleUrl(mainMenu.id, mainMenu.label);
//                AppContext.urlString = UrlManager.getRootUrl()
//                        + UrlTools.getTabsUrl(menuTabModel, mainMenu);
//                MenuManager.getInstance().enterMenuDetailOrList(menuTabModel,
//                        getActivity());
//            }
//        } catch (Exception e) {
//            // TODO: handle exception
//        }
//    }

    //创建contextdialog
    public static Dialog showContext(final Context context) {
        View view = LayoutInflater.from(context).inflate(R.layout.dialog_context,
                null);
        final AlertDialog builder = new AlertDialog.Builder(context).create();
        builder.setCancelable(true);
        builder.setView(view);

        Button btnsaoyisao = (Button) view.findViewById(R.id.btnsaoyisao);
        Button btnsaomiao = (Button) view.findViewById(R.id.btnsaomiao);

        btnsaoyisao.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View arg0) {
//				Intent intent = new Intent(context, CaptureActivity.class);
//				 context.startActivityForResult(intent, 10001);
                builder.cancel();
            }


        });
        btnsaomiao.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View arg0) {
                Intent intent = new Intent(context,
                        BusinesscardScanningActivity.class);
                context.startActivity(intent);
                builder.cancel();
            }
        });
        builder.show();
        return builder;
    }


    /**
     * 刷新
     *
     * @param event
     */
    public void onEventMainThread(RefreshMainMenuEvent event) {
        initData();
    }

    public void onEventMainThread(shouyerefrence event) {
        frameLayout = (FrameLayout) findViewById(R.id.tishi);
        currentShowList = new ArrayList<MenuTabModel>();
        currentShowList = event.list;
        if (currentShowList.size() <= 0) {
            frameLayout.setVisibility(View.VISIBLE);
        } else {
            frameLayout.setVisibility(View.GONE);
        }
        if (currentShowList.get(currentShowList.size() - 1).objId != addmore.objId) {//显示的list集合里面不包含addmore
            currentShowList.add(addmore);
        }
//        mAdapter.changeData(currentShowList);
        mAdapter.changeRecycleData(currentShowList);
        datasave.setDataList(RunTimeManager.getInstance().getUserId(), currentShowList);
        SharedPreferences pref = mContext.getSharedPreferences(RunTimeManager.getInstance().getUserId(), 0);
        Editor editor = pref.edit();
        editor.putBoolean("isFirstIn", false);
        editor.commit();
    }

    /**
     * 获得主页的默认显示menu
     *
     * @return
     */
    public MenuModel getMainMenu() {
        MenuModel menuModel = ((MainUIActivity) getActivity()).getCCMenuItems();
//		Log.d("fdwjf","方法"+menuModel.tab.size());
        return menuModel;
    }

    @Override
    public void onDestroy() {
        EventEngine.uregister(this);
        super.onDestroy();
    }

    @Override
    public void onResume() {
        // TODO Auto-generated method stub
        super.onResume();
        //跟这个类没有关系，用来判断是否退出了待审批，让他加载更多只取15
        if (!NetStateUtils.isNetworkConnected(getActivity())) {
            Toast.makeText(getActivity(), R.string.zanwus, Toast.LENGTH_SHORT).show();
        }
        CApplication.approNum = 1;
        request();
        MessageSetNCL();
    }

    String daiNum = null;

    public void onEventMainThread(MainApproNum event) {
        request();
    }

    public void request() {
        HttpUtils utils = new HttpUtils();
        RequestParams params = new RequestParams();
        params.addBodyParameter("binding", RunTimeManager.getInstance()
                .getServerBinding());
        params.addBodyParameter("serviceName", "getApprovalPendingSize");

        utils.send(HttpMethod.POST, UrlManager.getInterfaceUrl(), params,
                new RequestCallBack<String>() {

                    @Override
                    public void onFailure(HttpException arg0, String arg1) {
                        Log.d("-------------------------", "Failure。。。"
                                + arg1);
                    }

                    @Override
                    public void onSuccess(ResponseInfo<String> arg0) {
                        Log.d("-------------------------", "success。。。"
                                + arg0.result);
                        Gson gson = new Gson();
                        mydaishenpi ss = gson.fromJson(arg0.result,
                                mydaishenpi.class);
                        if ("true".equals(ss.result)) {
                            daiNum = ss.data.aprvpendingsize;
                            RunTimeManager.setDaiNum(daiNum);
                            mAdapter.notifyDataSetChanged();
                        }
                    }
                });
    }

    public void isLogin() {
        Boolean isFirstIn = false;
        SharedPreferences pref = mContext.getSharedPreferences("islogin", 0);
        Editor editor = pref.edit();
        editor.putBoolean("islogin", true);
        editor.commit();
    }

    public void MessageSetNCL() {
        long messagenum = 0;
        messagenum = RunTimeManager.getInstance().getMessageNx();
        message_num_tz.setText(messagenum + "");

        if (messagenum > 0) {
            message_num_tz.setVisibility(View.VISIBLE);
            if (messagenum > 99) {
                message_num_tz.setVisibility(View.INVISIBLE);
                message_num_99.setVisibility(View.VISIBLE);
            }

        } else {
            message_num_tz.setVisibility(View.INVISIBLE);
            message_num_99.setVisibility(View.INVISIBLE);
        }

    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        // TODO: inflate a fragment view
        View rootView = super.onCreateView(inflater, container, savedInstanceState);
        ButterKnife.bind(this, rootView);
        return rootView;
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        ButterKnife.unbind(this);
    }

    @OnClick(R.id.backCustoms)
    public void onClick() {
        Intent intent = new Intent(getActivity(), GongniuCheckinActivity.class);
        getActivity().startActivity(intent);

    }
}
